package com.easyhouse.websocket.controller;
import com.alibaba.fastjson.JSONObject;
import com.easyhouse.websocket.service.WebSocket;
import com.easyhouse.websocket.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class WebSocketController {
    @Autowired
    private WebSocket webSocket;

    @Autowired
    private WebSocketService webSocketService;

    @ResponseBody
    @RequestMapping("/sendTo")
    public String sendTo(@RequestParam("userId") String userId, @RequestParam("msg") String msg) throws IOException {
        webSocketService.setJsonString(userId, msg);
        webSocket.sendMessageTo(msg,userId);
        return "推送成功";
    }

    @ResponseBody
    @RequestMapping("/sendAll")
    public String sendAll(@RequestParam("msg") String msg) throws IOException {
        String fromUserId="system";//其实没用上
        webSocketService.setJsonString(fromUserId, msg);
        webSocket.sendMessageAll(msg,fromUserId);
        return "推送成功";
    }

    @ResponseBody
    @RequestMapping("/getmessage")
    public String getmessage(@RequestParam("userId") String userId){
       return webSocketService.getJsonString(userId);
    }



}